package com.online_video.servlet.client;

import com.google.gson.Gson;
import com.online_video.entity.*;
import com.online_video.service.CommentService;
import com.online_video.service.VideoService;
import com.online_video.service.impl.CommentServiceImpl;
import com.online_video.service.impl.VideoServiceImpl;
import com.online_video.servlet.BaseServlet;
import com.online_video.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet("/client/video.do")
public class VideoServlet extends BaseServlet {
VideoService videoService = new VideoServiceImpl();
CommentService commentService = new CommentServiceImpl();
Video video = new Video();
    public void insert(HttpServletRequest request, HttpServletResponse response) throws IOException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        HttpSession session = request.getSession(false);
        User user = (User)session.getAttribute("user");
        System.out.println(user);
        Integer vid = null;
        String viName = request.getParameter("videoName");
        Integer id=null;
        //未获得用户信息
        if (user==null){
            Gson gson = new Gson();
            String stateCode = gson.toJson("-2");
            response.getWriter().write(stateCode);
            return;
        }
        id = user.getId();
        Integer playAmount = 0;
        String category = request.getParameter("category");
        Long like = 0L;
        Long collect = 0L;
        String date = simpleDateFormat.format(new Date());
        String videoPath = request.getParameter("videoPath");
        String img = request.getParameter("videoCover");
        String describe = request.getParameter("describe");
        if(viName!=null&&category!=null&videoPath!=null&&img!=null&&describe!=null){
            video=new Video(vid,viName,id,playAmount,category,like,collect,date,videoPath,img,describe);
            int insert = videoService.insert(video);
            if (insert>0){
                //插入成功
                Gson gson = new Gson();
                String stateCode = gson.toJson("1");
                response.getWriter().write(stateCode);
                return;
            }else {
                //插入失败
                Gson gson = new Gson();
                String stateCode = gson.toJson("0");
                response.getWriter().write(stateCode);
                return;
            }
        }else{
            //前端输入为空
            Gson gson = new Gson();
            String stateCode = gson.toJson("-1");
            response.getWriter().write(stateCode);
            return;

        }
    }
    /*由视频的分类查询*/
    public void queryAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String category = request.getParameter("category");
        List<Video> videoList = videoService.queryByCategory(category);

        Gson gson = new Gson();
        String jsonStr = gson.toJson(videoList);

        PrintWriter writer = response.getWriter();
        writer.write(jsonStr);

    }

    /**
     * 由当前上传视频的用户信息进行查询
     * @param request
     * @param response
     * @throws IOException
     */
    public void queryByU_id(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String req = request.getParameter("u_id");
        if (req!=null&&req!=""){
            Integer u_id = Integer.parseInt(req);
            List<Video> videos = videoService.queryByU_id(u_id);
            if (videos!=null){
                Gson gson = new Gson();
                String toJson = gson.toJson(videos);
                response.getWriter().write(toJson);
            }
        }else {
            Gson gson = new Gson();
            String toJson = gson.toJson("0");
            response.getWriter().write(toJson);
        }

    }

    /**
     * 由名称进行模糊查询
     * @param request
     * @param response
     * @throws IOException
     */
    public void queryByViname(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String viname = request.getParameter("Viname");
        List<Video> videos = videoService.queryByViName(viname);
        if(videos!=null){

            Gson gson = new Gson();
            String jsonStr = gson.toJson(videos);

            PrintWriter writer = response.getWriter();
            writer.write(jsonStr);
        }


    }

    /**
     * 由视频的id编号进行精确的查询
     * @param request
     * @param response
     * @throws IOException
     */
    public void queryByVid(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer vid = Integer.parseInt(request.getParameter("vid"));
        Video video = videoService.queryByVid(vid);
        String stateCode ="";
        if (video!=null){
            //已找到该视频
            Gson gson = new Gson();
            String toJson = gson.toJson(video);
            response.getWriter().write(toJson);
        }else{
            //未找到该视频
            stateCode="0";
            Gson gson = new Gson();
            String toJson = gson.toJson(stateCode);
            response.getWriter().write(toJson);
        }

    }

    public void queryCollectBookById(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute("user");
        if (user != null){
            List<CollectBook> collectBooks = videoService.queryCollectById(user.getId());
            Gson gson = new Gson();
            String jsonStr = gson.toJson(collectBooks);
            response.getWriter().write(jsonStr);
        }

    }

    public void isExitCollectBook(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String collectName = request.getParameter("collectName");
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute("user");
        boolean isExitCollectBook = videoService.isExitCollectBook(user.getId(), collectName);
        if (isExitCollectBook){
            response.getWriter().write("-1");
        }else {
            CollectBook collectBook = new CollectBook(null,user.getId(),collectName);
            int result = videoService.insertBookCollect(collectBook);
            if (result > 0){
                response.getWriter().write("1");
            }
        }
    }

    public void collectBook(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute("user");
        if (request.getParameter("c_book_id") != null){
            Integer collectName = Integer.parseInt(request.getParameter("c_book_id"));
            List<CollectBook> collectBooks = videoService.queryCollect(user.getId(), collectName);
            Gson gson = new Gson();
            String jsonStr = gson.toJson(collectBooks);
            response.getWriter().write(jsonStr);
        }
    }

    public void insertCollect(HttpServletRequest request, HttpServletResponse response) throws IOException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String c_date = simpleDateFormat.format(new Date());
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute("user");
        Integer vid = Integer.parseInt(request.getParameter("v_id"));
        Integer c_book_id = Integer.parseInt(request.getParameter("c_book_id"));

        System.out.println();
        Collect collect = new Collect(vid,user.getId(),c_book_id,c_date);
        int result = videoService.insertCollect(collect);
        if (result > 0){
            response.getWriter().write("1");
        }else {
            response.getWriter().write("-1");
        }
    }

    public void delCollectVideo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute("user");
        Integer collectid = Integer.parseInt(request.getParameter("collectid"));
        int result = videoService.delCollectVide(collectid);
        if (result > 0){
            response.getWriter().write("1");
        }else {
            response.getWriter().write("-1");
        }
    }

    public void insertComment(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer vid = Integer.parseInt(request.getParameter("vid"));
        Integer u_id = Integer.parseInt(request.getParameter("u_id"));
        String comments = request.getParameter("comment");
        String date = request.getParameter("date");
        Comment comment = new Comment(null,vid,u_id,comments,date);
        int insert = commentService.insert(comment);
        String stateCode = "";
        if (insert>0){
            //评论插入成功
            stateCode = "1";
            Gson gson = new Gson();
            String state = gson.toJson(stateCode);
            response.getWriter().write(state);
        }else {
            //评论插入失败
            stateCode = "0";
            Gson gson = new Gson();
            String state = gson.toJson(stateCode);
            response.getWriter().write(state);
        }
    }

    public void queryComment(HttpServletRequest request, HttpServletResponse response) throws IOException {
       Integer vid = Integer.parseInt(request.getParameter("vid"));
        List<Comment> comments = commentService.queryByVid(vid);
        if (comments!=null){
            Gson gson = new Gson();
            String toJson = gson.toJson(comments);
            response.getWriter().write(toJson);
        }

    }

    public void isExitCollectVideo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute("user");

        Integer vid = Integer.parseInt(request.getParameter("v_id"));
        int isExitCv = videoService.isExitCollectVideo(vid, user.getId());
        response.getWriter().print(isExitCv);

    }

    public void cancelCollectVideo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute("user");
        Integer vid = Integer.parseInt(request.getParameter("v_id"));
        int result = videoService.cancelCollectVideo(vid, user.getId());
        response.getWriter().print(result);
    }

    public void countCollect(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer vid = Integer.parseInt(request.getParameter("v_id"));
        int countCollect = videoService.countCollect(vid);
        response.getWriter().print(countCollect);
    }

    public void page(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //工具类转型和默认值
        String parm = request.getParameter("parm");
        String category = request.getParameter("category");

        int pageNo = WebUtils.parseInt(request.getParameter("pageNo"), 1);
        int pageSize = WebUtils.parseInt(request.getParameter("pageSize"), VideoPage.DEFAULT_PAGE_SIZE);
        VideoPage<Video> page = videoService.page(pageNo, pageSize, parm,category);

//        Gson gson = new Gson();
//        String jsonStr = gson.toJson(page);
//        response.getWriter().write(jsonStr);

        request.setAttribute("page",page);
        request.getRequestDispatcher("./video_list.jsp").forward(request,response);

    }

    public void queryAllVideoByUid(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute("user");
        List<Video> videoList = videoService.queryByU_id(user.getId());
        Gson gson = new Gson();
        String toJson = gson.toJson(videoList);
        response.getWriter().write(toJson);
    }

    public void delVideoByVid(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer vid = Integer.parseInt(request.getParameter("vid"));
        int delete = videoService.delete(vid);
        response.getWriter().print(delete);
    }


    public void queryUserByVid(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer vid = Integer.parseInt(request.getParameter("vid"));
        User user = videoService.queryUserByVid(vid);
        Gson gson = new Gson();
        String jsonStr = gson.toJson(user);
        response.getWriter().write(jsonStr);
    }
}
